<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">

    <body>

        <ui:composition template="./../../Template/generalTemplate.xhtml">

            <ui:define name="content">

                <h:form id="form1">
                    <f:view beforePhase="#{manageInvoiceManagedBean.initView}">
                        <p:panel style="height:495px;" header="Invoice Detail" >
                            <p:commandLink style="margin-left:5px; position:relative;float:left; height:5px; width:300px;" value="Commence Dunning Email" action="#{manageInvoiceManagedBean.sendDunningEmail}"/>
                            <p:commandLink style="margin-left:600px; position:relative;" value ="View All Invoice" action="#{manageInvoiceManagedBean.viewAll}"/>
                            <hr/>
                            <div style="height:375px; overflow: auto; border-bottom:solid #AFAFAF 1px;">

                                <p:growl id="growl1" showDetail="true" /> 
                                <h:outputText style="color:red; font-size:15px;" value="The Invoice Is Abolished" rendered="#{manageInvoiceManagedBean.invoice.abolished}"/> <br/> 

                                <h:panelGrid columns ="2" cellpadding="3">
                                    <h:outputText style="color:blue; font-size:13px;" value ="Payment is Needed" rendered ="#{manageInvoiceManagedBean.isPaymentneed}"/> 
                                    <h:outputText value ="" rendered ="#{manageInvoiceManagedBean.isPaymentneed}"/> 
                                    <h:outputText style="color:green; font-size:13px;" value ="Payment Cleaned" rendered ="#{!manageInvoiceManagedBean.isPaymentneed}"/> 
                                    <h:outputText value ="" rendered ="#{!manageInvoiceManagedBean.isPaymentneed}"/> 
                                    <h:outputText value="Invoice Id:"/>
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.id}"/> 
                                    <h:outputText value="Issued By:"/>
                                    <h:outputText value="#{manageInvoiceManagedBean.employeeName}"/> 
                                    <h:outputText value= "Issuing Date"/> 
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.issuing_date}"> 
                                        <f:convertDateTime pattern ="yyyy-MM-dd"/>
                                    </h:outputText>
                                    <h:outputText value= "Due Date"/> 
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.dueDate}"> 
                                        <f:convertDateTime pattern ="yyyy-MM-dd"/>
                                    </h:outputText>
                                    <h:outputText style="color:red;" value="Expired" rendered="#{manageInvoiceManagedBean.idDued}"/>
                                    <h:outputText value="" rendered="#{manageInvoiceManagedBean.idDued}"/>   
                                    <h:outputText value="InvoiceType:"/>
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.invoiceType}"/> 
                                    <h:outputText value= "Total Due:"/>
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.amountPayable}"/> 
                                    <h:outputText value= "Amount Payable:"/>
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.payment_balance}"/> 
                                    <h:outputText value= "Amount Paid:"/>
                                    <h:outputText value="#{manageInvoiceManagedBean.invoice.amountPaid}"/>
                                    <h:outputText value="Receipt:"/>
                                    <h:outputText value="No Receipt Related" rendered="#{!manageInvoiceManagedBean.receiptIssued}"/>
                                    <p:commandLink value="Show" rendered="#{manageInvoiceManagedBean.receiptIssued}" oncomplete="dlg3.show()"/>
                                    <h:outputText value="Dunning Emails: "/>
                                    <h:outputText value="No Dunning Email Related" rendered="#{!manageInvoiceManagedBean.hasDunningEmail}"/>
                                    <p:commandLink value="Show" rendered="#{manageInvoiceManagedBean.hasDunningEmail}" oncomplete="dlg4.show()"/>
                                </h:panelGrid>

                                <p:fieldset legend="Delivery Order Info" toggleable="true" toggleSpeed="500"  
                                            toggleListener="#{manageInvoiceManagedBean.handleToggle}"  
                                            onToggleUpdate="deliveryInfo" collapsed="true">  

                                    <p:dataTable style ="margin-top:10px;" value ="#{manageInvoiceManagedBean.formDeliveryOrderList()}" var="o">
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="DeliveryOder #" />
                                            </f:facet>
                                            <h:outputText value="#{o.id}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Line Item"/>
                                            </f:facet>
                                            <p:commandLink value="Show" onclick="dlg2.show()"/>
                                            <p:dialog header="DelvieryOrder LineItemList" widgetVar="dlg2" resizable="true" width="500">  
                                                <p:dataTable value="#{manageInvoiceManagedBean.dLineItem}" var="lineItem">
                                                    <p:column>
                                                        <f:facet name="header">Product</f:facet>
                                                        #{lineItem.product.productName}
                                                    </p:column>  
                                                    <p:column>
                                                        <f:facet name="header">Quantity</f:facet>
                                                        #{lineItem.quantity}
                                                    </p:column>  
                                                    <p:column>
                                                        <f:facet name="header">Unit Price</f:facet>
                                                        #{lineItem.price}
                                                    </p:column>  
                                                    <p:column>
                                                        <f:facet name="header">Total Price</f:facet>
                                                        #{lineItem.cumAmount}
                                                    </p:column>  

                                                </p:dataTable>   
                                            </p:dialog>  
                                        </p:column>    
                                        <p:column>
                                            <f:facet name="header">Delivery Date</f:facet>
                                            <h:outputText value="#{o.deliveryDate}">
                                                <f:convertDateTime pattern ="dd/MM/yyyy"/>
                                            </h:outputText>    
                                        </p:column>

                                        <p:column>
                                            <f:facet name="header">Delivery Location</f:facet>
                                            <h:outputText value="#{o.shippingAddress.streetName},"/>
                                            <h:outputText value="#{o.shippingAddress.city}"/>
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">Payment Status</f:facet>
                                            <h:outputText value="Paid" rendered ="#{o.paymentStatus}"/>
                                            <h:outputText value="Not Paid" rendered="#{!o.paymentStatus}"/>
                                        </p:column> 
                                    </p:dataTable> 

                                </p:fieldset>  

                                <p:commandButton style="margin-top:10px;" value="Update Payment" onclick="dlg.show()"/>
                                <p:commandButton style="margin-top:10px; margin-right:10px; float:right; position:relative;" value="Correct PaymentStatus" update="growl1 form1" action="#{manageInvoiceManagedBean.correctDeliveryOrderStatus}"/><br/>
                            </div>
                            <p:commandButton style="margin-top:20px; " value="Abolish Invoice" onclick="confirmation.show()" rendered="#{!manageInvoiceManagedBean.invoice.abolished}"/> 

                            <p:commandButton style="margin-top:20px;" value="Remove" update="form1" action="#{manageInvoiceManagedBean.removeInvoice}" rendered="#{manageInvoiceManagedBean.invoice.abolished}"/>
                            <p:commandButton style="margin-top:20px; margin-right:10px; float:right; position:relative;" value="Issue Receipt" update="growl1" action="#{manageInvoiceManagedBean.issueReceipt}"/>






                            <p:confirmDialog message="Are you sure to abolish the invoice?"  
                                             showEffect="bounce" hideEffect="explode"  
                                             header="Abolish Invoice" severity="alert" widgetVar="confirmation">  

                                <p:commandButton value="Yes Sure" update="form1" ajax="true" oncomplete="confirmation.hide()"  
                                                 action="#{manageInvoiceManagedBean.abolishInvoice}" />  
                                <p:commandButton value="Not Yet" onclick="confirmation.hide()" type="button" />   

                            </p:confirmDialog>
                            <p:dialog header="Fill in Amount Paid by Customer" widgetVar="dlg" resizable="false">  


                                <h:panelGrid columns="2" style="margin-bottom:10px">  
                                    <h:outputText value="Amount:" />  
                                    <p:inputText value="#{manageInvoiceManagedBean.paymentAmount}" />  
                                </h:panelGrid>  

                                <p:commandButton value="Submit" ajax="false" action="#{manageInvoiceManagedBean.updatePayment}" update="form1" oncomplete="dlg.hide();"/>  
                            </p:dialog> 
                            <p:dialog widgetVar="dlg3" header="Receipt Detail" width="600">
                                <p:outputPanel style="margin-top:10px; margin-left:5px; width:800px;">   
                                    <h:panelGrid columns="2" cellpadding ="5">
                                        <h:outputText value="Receipt ID:"/>
                                        <h:outputText value="#{manageInvoiceManagedBean.invoice.receipt.id}"/>
                                        <h:outputText value="Issued By:"/>
                                        <h:outputText value="#{manageInvoiceManagedBean.employeeName}"/>
                                        <h:outputText value="Issued Date:"/>
                                        <h:outputText value="#{manageInvoiceManagedBean.invoice.receipt.issuingDate}">
                                            <f:convertDateTime pattern ="dd/MM/yyyy"/>
                                        </h:outputText>   
                                        <h:outputText value="Issued To:"/>
                                        <h:outputText value="#{manageInvoiceManagedBean.compnayName}"/>
                                        <h:outputText value="Amount Paid:"/>
                                        <h:outputText value="#{manageInvoiceManagedBean.invoice.amountPaid}"/>
                                    </h:panelGrid>   

                                </p:outputPanel>
                            </p:dialog>

                            <p:dialog widgetVar="dlg4" header="Dunning Email Detail" width="600">
                                <p:outputPanel style="margin-top:10px; margin-left:5px; width:800px;">   
                                    <p:dataTable value="#{manageInvoiceManagedBean.invoice.dunningEmails}" var="dEmail">
                                        <p:column>
                                            <f:facet name="header">Dunning Email ID</f:facet>
                                            #{dEmail.id}
                                        </p:column>  
                                          <p:column>
                                            <f:facet name="header">Dunning Level</f:facet>
                                            #{dEmail.dunningLevel}
                                        </p:column>  
                                        <p:column>
                                            <f:facet name="header">Email Detail</f:facet>
                                            #{dEmail.content}
                                        </p:column>  
                                        <p:column>
                                            <f:facet name="header">Send Date</f:facet>
                                            <h:outputText value="#{dEmail.sendDate}">
                                                <f:convertDateTime pattern ="yyyy-MM-dd"/>
                                            </h:outputText>
                                        </p:column>  
                                        <p:column>
                                            <f:facet name="header">Due Date</f:facet>
                                            <h:outputText value="#{dEmail.dueDate}">
                                                <f:convertDateTime pattern ="yyyy-MM-dd"/>
                                            </h:outputText>
                                        </p:column>  
                                    </p:dataTable>

                                </p:outputPanel>
                            </p:dialog>

                        </p:panel>
                    </f:view>  
                </h:form>

            </ui:define>


        </ui:composition>

    </body>
</html>
